package com.harboursoftware.xstorage.operations;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ListMultimap;
import com.harboursoftware.xstorage.XStorageException;
import com.harboursoftware.xstorage.ac.Permission;
import com.harboursoftware.xstorage.db.BucketDao;
import com.harboursoftware.xstorage.db.DaoManager;
import com.harboursoftware.xstorage.model.XBucket;
import com.harboursoftware.xstorage.model.XElement;
import com.harboursoftware.xstorage.model.XUser;
import com.harboursoftware.xstorage.results.ErrorResult;
import com.harboursoftware.xstorage.results.GetServiceResult;
import com.harboursoftware.xstorage.results.Result;

public class GetService extends Operation {
    private final static Logger log = LoggerFactory.getLogger(GetService.class);
    private BucketDao bucketDao = DaoManager.getInstance().getBucketDao();

    @Override
    public Result execute(XUser requester, XElement element, ListMultimap<String, String> headers,
        Map<String, String> parameters) {
        log.debug("execute");
        if (requester == null || requester.getId() == null) {
            return ErrorResult.create(ErrorResult.Type.ACCESS_DENIED);
        }
        List<XBucket> buckets = null;
        try {
            buckets = bucketDao.findBucketsOfUser(requester);
        } catch (XStorageException e) {
            return ErrorResult.create(ErrorResult.Type.INTERNAL_ERROR);
        }
        GetServiceResult result = new GetServiceResult(requester, buckets);
        return result;
    }

    @Override
    public Permission getRequiredPermission() {
        //no need permission.
        return null;
    }
}
